package com.ym9527.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ym9527.controller.apiEntity.PunchCardAE;
import com.ym9527.entity.Course;
import com.ym9527.entity.CourseStudent;
import com.ym9527.entity.PunchCardRecord;
import com.ym9527.entity.User;
import com.ym9527.jpa.CourseJpa;
import com.ym9527.jpa.CourseStudentJpa;
import com.ym9527.jpa.PunchCardRecordJpa;
import com.ym9527.jpa.UserJpa;
import com.ym9527.utils.R;
import com.ym9527.utils.interfaceS.MapCorrelation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 这是学生相关接口
 */
@RestController
@RequestMapping("student")
public class StudentRelevantController {

    @Resource
    MapCorrelation mapCorrelation;

    @Resource
    CourseStudentJpa courseStudentJpa;

    @Resource
    CourseJpa courseJpa;

    @Resource
    UserJpa userJpa;

    @Resource
    PunchCardRecordJpa punchCardRecordJpa;

    /**
     * 这是获取某个学生的全部课程的接口
     * @return
     */
    @GetMapping("getAllCourse")
    @Transactional()
    public R getAllCourse(HttpServletRequest request){

        /**
         *
         * 这里要获取是否打卡的信息
         *
         */


        String studentName=request.getHeader("user").split("-")[0];

        User user=userJpa.findByUserName(studentName);

        List<CourseStudent> courseStudentList=courseStudentJpa.findByStudentId(user.getUserId());



        List<Map> courseList=new LinkedList<>();

        courseStudentList.forEach(item->{
            Map courseMap=new HashMap<>();

            String dateString=new String();
            SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
            dateString=f.format(new Date());

            Course course=courseJpa.getById(item.getCourseId());

            PunchCardRecord punchCardRecord=this.punchCardRecordJpa.findByPunchCardStudentIdAndDateStringAndPunchCardCourseId(user.getUserId(),dateString,course.getCourseId());
            courseMap.put("courseInfo",course);
            courseMap.put("ifPunchCard",null!=punchCardRecord);

            courseList.add(courseMap);
        });

        return R.ok(courseList);
    }

    /**
     * 这是学生打卡接口
     * @return
     */
    @PostMapping("punchCard")
    @Transactional()
    public R PunchCard(HttpServletRequest request,@RequestBody PunchCardAE punchCardAE){

        String dateString=new String();
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
        dateString=f.format(new Date());

        String studentName=request.getHeader("user").split("-")[0];

        User user=userJpa.findByUserName(studentName);

        PunchCardRecord punchCardRecordZ=punchCardRecordJpa.findByPunchCardStudentIdAndDateStringAndPunchCardCourseId(user.getUserId(),dateString,punchCardAE.getCourseId());

        if(null!=punchCardRecordZ){
           return R.error("不允许重复打卡");
        }

        Course course=this.courseJpa.getById(punchCardAE.getCourseId());
        //这里进行打卡时间的判断
        String week=getWeekOfDate(new Date());
        if(week.equals(course.getPunchCardWeek())){

        }else{
            return R.error("日期未到");
        }

        String dateString2=new String();
        SimpleDateFormat f2=new SimpleDateFormat("HH:mm");
        dateString2=f2.format(new Date());

        Date data1 = DateUtil.parse("2022-09-07 "+dateString2+":00");
        Date date2 = DateUtil.parse("2022-09-07 "+course.getPunchCardDate()+":00");

        if(DateUtil.between(data1, date2, DateUnit.MINUTE)>5){
            return R.error("时间未到");
        }




        /**
         * 这里进行范围判断
         */
        if(mapCorrelation.ScopeCalculate(punchCardAE.getLongitude(),punchCardAE.getLatitude(),course.getPunchCardScope(), course.getLongitude(),course.getLatitude())){

        }else{
            return R.error("当前位置不对");
        }


        PunchCardRecord punchCardRecord=new PunchCardRecord();

        punchCardRecord.setPunchCardStudentId(user.getUserId());
        punchCardRecord.setPunchCardCourseId(punchCardAE.getCourseId());
        punchCardRecord.setPunchCardTime(new Date());

        punchCardRecord.setDateString(dateString);


        punchCardRecordJpa.save(punchCardRecord);
        return R.ok("打卡成功");
    }



    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"天", "一", "二", "三", "四", "五", "六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }



}
